import {Module} from 'vuex';
import {getPageListData,deletePageData,createPageData,editPageData} from '@/service/main/system/system'
const systemModule={
    namespaced: true,
    state(){
        return{
            userList:[],
            userCount:0,
            roleList:[],
            roleCount:0,
            goodsList:[],
            goodsCount:0,
            menuList:[],
            menuCount:0
        }
    },
    getters:{
      pageListData(state:any){
          return (pageName:string)=>{
             return state[`${pageName}List`]
          }
      },
      pageListCount(state:any){
        return (pageName:string)=>{
           return state[`${pageName}Count`]
        }
    }
    },
    mutations:{
        changeUserList(state,userList){
            state.userList=userList
        },
        changeUserCount(state,userCount){
            state.userCount=userCount
        },
        changeRoleList(state,list){
            state.roleList=list
        },
        changeRoleCount(state,count){
            state.roleCount=count
        },
        changeGoodsList(state,list){
            state.goodsList=list
        },
        changeGoodsCount(state,count){
            state.goodsCount=count
        },
        changeMenuList(state,list){
            state.menuList=list
        },
        changeMenuCount(state,count){
            state.menuCount=count
        }
    },
    actions:{
        // 请求用户数据
      async  getPageListAction({ commit },payload:any){
       
         
        //1获取 pageName
        const pageName=payload.pageName
        let pageUrl=''
        // console.log(pageName); //user
        
        switch (pageName) {
            case 'user':
                pageUrl='/users/list'
                break;
           case 'role':
                 pageUrl='/role/list'
                 break;
            case 'goods':
                 pageUrl='/goods/list'
                 break;
            case 'menu':
                 pageUrl='/menu/list'
                break;
        }
        //   2 对页面发生请求
        const pageResult=await getPageListData(pageUrl,payload.queryInfo)
        // console.log(pageResult.data);
        
        // 3  数据存在State中  
        const {list,totalCount}=pageResult.data

        
        switch (pageName) {
            case 'user':
                commit(`changeUserList`,list)
                commit(`changeUserCount`,totalCount)
                break;
           case 'role':
                commit(`changeRoleList`,list)
                commit(`changeRoleCount`,totalCount)
                 break;
          case 'goods':
                 commit(`changeGoodsList`,list)
                 commit(`changeGoodsCount`,totalCount)
                break;
                case 'menu':
                 commit(`changeMenuList`,list)
                 commit(`changeMenuCount`,totalCount)
                   break;
            default:
                break;
         } 
        }, 

       async deletePageDataAction({ dispatch },payload:any){
            const {pageName,id}=payload
            const pageUrl=`/${pageName}/${id}`
            // console.log(pageName);
            
            await deletePageData(pageUrl)   
            dispatch('getPageListAction',{
                pageName,
                queryInfo:{
                    offset:0,
                    size:10
                }
            })
       },

       async createPageDataAction( {dispatch},payload:any) {
        // 创建数据的请求
           const {pageName,newData}=payload
           const pageUrl=`/${pageName}`
           await createPageData(pageName,pageUrl)
        //请求最新的数据  
        dispatch('getPageListAction',{
            pageName,
            queryInfo:{
                offset:0,
                size:10
            }
        })
       },
       async  editPageDataAction( {dispatch},payload:any) {
        // 编辑数据的请求
           const {pageName,editData,id}=payload
           const pageUrl=`/${pageName}/${id}`
           await editPageData(pageName,editData)
        //请求最新的数据  
        dispatch('getPageListAction',{
            pageName,
            queryInfo:{
                offset:0,
                size:10
            }
        })
       },
     
}
}
export default systemModule
